package com.work.farm.controller;

import com.github.pagehelper.Page;
import com.work.entity.PageResult;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import com.work.farm.pojo.TCustFarmlandEntity;
import com.work.farm.pojo.TCustFarmlandHistoryEntity;
import com.work.farm.service.FarmLandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/farmland")
public class FarmLandController {

    @Autowired
    private FarmLandService farmLandService;

    //添加大田
    @PostMapping("/addFarmLand")
    public Result addFarmLand(@RequestBody TCustFarmlandEntity tCustFarmlandEntity){
        try{
            //调用
            farmLandService.addFarmLand(tCustFarmlandEntity);
            return new Result(true, StatusCode.ADD_OK,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ADD_ERROR,"添加失败");
        }
    }

    //删除大田
    @DeleteMapping("/deleteFarmLand")
    public Result deleteFarmLand(@RequestParam("id")Integer id){
        try{
            //调用
            farmLandService.deleteFarmLand(id);
            return new Result(true, StatusCode.DELETE_OK,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.DELETE_ERROR,"删除失败");
        }
    }

    //修改大田信息
    @PutMapping("/updateFarmLand")
    public Result updateFarmLand(@RequestBody TCustFarmlandEntity tCustFarmlandEntity){
        try{
            //调用
            farmLandService.updateFarmLand(tCustFarmlandEntity);
            return new Result(true, StatusCode.UPDATE_Ok,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.UPDATE_ERROR,"修改失败");
        }
    }

    //分页查询大田信息
    @GetMapping("/findByPage")
    public Result findByPage(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5",required = false)Integer pageSize,
                             @RequestParam(required = false) Map searchMap){
        try{
            Page<TCustFarmlandEntity> page = farmLandService.findByPage(pageNum, pageSize, searchMap);
            PageResult<TCustFarmlandEntity> pageResult = new PageResult<>(page.getTotal(), page.getResult());
            return new Result(true,StatusCode.FIND_Ok,"查询成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,StatusCode.FIND_ERROR,"查询失败");
        }
    }

    //查询大田详情信息
    @GetMapping("/findById")
    public Result findById(Integer id){
        try{
            Map<String,Object> map = farmLandService.findById(id);
            return new Result(true,StatusCode.FIND_Ok,"查询成功",map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.FIND_ERROR,"查询失败");
        }
    }

    //查询大田历史信息
    @GetMapping("/findHistory")
    public Result findHistory(Integer fid){
        try {
            Map<String,Object> map= farmLandService.findById(fid);
            List<TCustFarmlandHistoryEntity> history= farmLandService.findHistory(fid);
            map.put("history",history);
            return new Result(true,StatusCode.FIND_Ok,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.FIND_ERROR,"查询失败");
        }
    }


}